package com.baidu.fastXML;

import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * json转对象
 */
public class TestJson {
    public static void main(String[] args) throws Exception {
        String jsonStr = "{\n" +
                "  \"eventVersion\":\"63\",\n" +
                "  \"eventId\":\"trail-event-iamusercreate-8e4b1d22131646ba92736c6a1dfc54a7-1652859532000\",\n" +
                "  \"eventType\":\"普通事件\",\n" +
                "  \"eventSource\":\"iam\",\n" +
                "  \"eventName\":\"CreateUser\",\n" +
                "  \"eventTimeInMilliseconds\":1652859532000,\n" +
                "  \"eventTime\":\"2022-05-18T07:38:52Z\",\n" +
                "\n" +
                "  \"userIdentity\":{\n" +
                "    \"iamDomainId\":\"33f35c23a0c848b5b9afb3ad35903f08\",\n" +
                "    \"loginUserId\":\"\",\n" +
                "    \"userDisplayName\":\"sunyumei_2\"\n" +
                "  },\n" +
                "  \"userIpAddress\":\"116.237.219.206\",\n" +
                "  \"userAgent\":\"Mozilla/5.0 (Macintosh; Intel Mac OS X 12_3_1) AppleWebKit/537.3\",\n" +
                "\n" +
                "  \"regionId\":\"bj\",\n" +
                "  \"requestId\":\"8e4b1d22131646ba92736c6a1dfc54a7\",\n" +
                "\n" +
                "  \"apiVersion\":\"1.0.0\",\n" +
                "  \"description\":\"\",\n" +
                "  \"errorCode\":\"200\",\n" +
                "  \"errorMessage\":\"\",\n" +
                "  \"eventDetail\":{\n" +
                "    \"additionalEventData\":{\n" +
                "      \"api\":\"POST /api/iam/user/create\",\n" +
                "      \"host\":\"bjhw-bce-online-product-console0.bjhw.baidu.com\",\n" +
                "      \"method\":\"POST\",\n" +
                "      \"cost\":\"120\"\n" +
                "    }\n" +
                "  },\n" +
                "\n" +
                "  \"resources\":[\n" +
                "    {\n" +
                "      \"resourceType\":\"resource-User\",\n" +
                "      \"resourceName\":\"fa8e87604637446c81df7e03ec053093\"\n" +
                "    }\n" +
                "  ],\n" +
                "\n" +
                "  \"success\":true\n" +
                "}\n";

        ObjectMapper objectMapper = new ObjectMapper();
        //1.json转对象
        Event event1 = objectMapper.readValue(jsonStr, Event.class);
        System.out.println(event1.getEventTime());

        //2.对象转json
        String s = objectMapper.writeValueAsString(event1);
        System.out.println(s);

    }
}
